<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "tag".
 *
 * @property int $id
 * @property string $name
 * @property int|null $frequency
 */
class Tag extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tag';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['frequency'], 'integer'],
            [['name'], 'string', 'max' => 128],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'frequency' => 'Frequency',
        ];
    }
    //用正则表达式把字符串转换成数组
    public static function string2array($tags){
        return preg_split('/\s*,\s*/',trim($tags),-1,PREG_SPLIT_NO_EMPTY);
    }
    //把数组转成字符串
    public static function array2string($tags){
        return implode(',',$tags);
    }
    //添加标签的操作
    public static function addTags($tags){
        print_r($tags);

        if(empty($tags)) return;
        foreach ($tags as $name){
            $atag = Tag::find()->where(['name'=>$name])->one();
            $atagCount = Tag::find()->where(['name'=>$name])->count();

            if(!$atagCount){
                $tag = new Tag();
                $tag->name = $name;
                $tag->frequency = 1;
                $tag->save();
            }else{
                $atag->frequency += 1;
                $atag->save();
            }
        }

    }
    //删除标签
    public static function removeTags($tags){

        if(empty($tags)) return;
        foreach($tags as $name){
            $atag = Tag::find()->where(['name'=>$name])->one();
            $atagCount = Tag::find()->where(['name'=>$name])->count();
            if($atagCount && $atag->frequency<=1){
                $atag->delete();
            }else{
                $atag->frequency -= 1;
                $atag->save();
            }
        }
    }
    //更新标签次数
    public static function updateFrequency($oldTags,$newTags){
        if(!empty($oldTags) || !empty($newTags)){
            $oldTagsArray = self::string2array($oldTags);
            $newTagsArray = self::string2array($newTags);
            self::addTags(array_values(array_diff($newTagsArray,$oldTagsArray)));
            self::removeTags(array_values(array_diff($oldTagsArray,$newTagsArray)));
        }
    }

}
